<template>
  <!-- 左边 -->
  <el-container>
    <!-- 左侧 -->
    <el-aside width="200px">
      <h2 class="el-icon-cloudy">商家管理中心</h2>
      <el-menu
        @select="selectMenu"
        :default-active="curactive"
        background-color="#034b73"
        text-color="#fff"
        active-text-color="#0f94bc"
        unique-opened
        router
      >
        <div v-for="obj in treelist" :key="obj.index">
          <!-- 可展开的菜单 -->
          <el-submenu v-if="obj.children" :index="obj.index">
            <template slot="title">
              <i :class="obj.i"></i>
              <span>{{ obj.text }}</span>
            </template>

            <!-- 循环里面childer数组 -->
            <el-menu-item
              v-for="child in obj.children"
              :key="child.index"
              :index="child.index"
              >{{ child.text }}</el-menu-item
            >
          </el-submenu>
          <!-- 无法展开的菜单 -->
          <el-menu-item v-else :index="obj.index">
            <i :class="obj.i"></i>
            <span slot="title">{{ obj.text }}</span>
          </el-menu-item>
        </div>
      </el-menu>
    </el-aside>

    <!-- 右侧 -->
    <el-container>
      <!-- 右-头 -->
      <el-header>
        <div>
          <el-breadcrumb separator="/">
            <el-breadcrumb-item v-for="item in breadlist" :key="item">{{
              item
            }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="el-header-right">
          <span>欢迎你,{{ myuser }}</span>
          <img :src="head" @click="clickimg" />
        </div>
      </el-header>
      <!-- 右-身 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { account, checktoken } from "../apis/users";
import { bus } from "@/utils/utils";

export default {
  data() {
    return {
      breadlist: [],
      treelist: [
        {
          index: "/index/main",
          i: "el-icon-star-off",
          text: "后台首页",
          bread: ["首页"],
        },
        {
          index: "/index/orderman",
          i: "el-icon-document",
          text: "订单管理",
          bread: ["订单管理"],
        },
        {
          index: "3",
          i: "el-icon-shopping-bag-1",
          text: "商品管理",
          children: [
            {
              index: "/index/itemlist",
              text: "商品列表",
              bread: ["商品管理", "商品列表"],
            },
            {
              index: "/index/itemadd",
              text: "商品添加",
              bread: ["商品管理", "商品添加"],
            },
            {
              index: "/index/itemtype",
              text: "商品分类",
              bread: ["商品管理", "商品分类"],
            },
          ],
        },
        {
          index: "/index/storemanager",
          i: "el-icon-box",
          text: "店铺管理",
          bread: ["店铺管理"],
        },
        {
          index: "5",
          i: "el-icon-user-solid",
          text: "账号管理",
          children: [
            {
              index: "/index/accountlist",
              text: "账号列表",
              bread: ["账号管理", "账号列表"],
            },
            {
              index: "/index/accountAdd",
              text: "添加账号",
              bread: ["账号管理", "添加账号"],
            },
            {
              index: "/index/changepwd",
              text: "修改密码",
              bread: ["账号管理", "修改密码"],
            },
          ],
        },
        {
          index: "6",
          i: "el-icon-pie-chart",
          text: "商品统计",
          children: [
            {
              index: "/index/itemstat",
              text: "商品销售",
              bread: ["商品统计", "商品销售"],
            },
            {
              index: "/index/orderstat",
              text: "订单统计",
              bread: ["商品统计", "订单统计"],
            },
          ],
        },
      ],
      curactive: "/index/main", //当前激活的地址

      myuser: "",
      head: "",
    };
  },
  methods: {
    clickimg() {
      this.$router.push("/index/Userinfo");
    },

    //面包屑功能 选中菜单点击事件
    selectMenu(index) {
      console.log(index);
      let bread = [];
      //循环菜单列表
      for (const obj of this.treelist) {
        // 判断是否有二级菜单
        if (obj.children) {
          // 有就循环children
          for (const child of obj.children) {
            if (child.index == index) {
              bread = child.bread; //拿到对应数组
            }
          }
          //否则直接判断index
        } else {
          if (obj.index == index) {
            bread = obj.bread;
          }
        }
      }
      //把找到的数据赋值给面包屑数组
      this.breadlist = bread;
    },
  },

  created() {
    //如果是普通管理员,则不显示更多的权限选项
    if (localStorage.role == "normal") {
      this.treelist.splice(3);
    }

    this.myuser = localStorage.user;

    checktoken({ token: localStorage.token }).then((res) => {
      // console.log(res);
      if (res.data.code == 0) {
        account({ id: localStorage.id }).then((res) => {
          // console.log(res);
          this.head = res.data.accountInfo.imgUrl;
        });
      } else {
        this.myuser = "请登录";
      }
    });

    this.curactive = location.hash.replace("#", "");
    // console.log((this.curactive = location.hash.replace("#", "")));
    //刷新初始化就把当前path赋给selectMenu
    this.selectMenu(this.curactive);

    bus.$on("photo", (val) => {
      this.head = val;
    });
  },
};
</script>

<style lang="less" scoped>
@import url("../assets/styles/themes.less");
.el-container {
  width: 100%;
  height: 100%;
  h2 {
    color: @sizecolor;
    margin: 10px;
    text-align: center;
  }
  .el-menu {
    border: 0;
  }
  .el-aside {
    background-color: @basecolor2;
    background-image: url(../assets/imgs/haixian.png);
    background-repeat: no-repeat;
  }

  .el-header {
    background-color: @basecolor1;
    display: flex;
    justify-content: space-between;
    align-items: center;
    span {
      color: #fff;
    }
    .el-header-right {
      display: flex;
      align-items: center;
      img {
        width: 60px;
        height: 60px;
        border-radius: 50%;
        margin-left: 20px;
      }
    }
    .el-breadcrumb /deep/ .el-breadcrumb__inner {
      color: #ccc !important; //你想要设置的字体颜色
    }
  }
  .el-submenu__title i {
    color: @sizecolor;
  }
  .el-menu-item i {
    color: @sizecolor;
  }
  .el-container {
    background-color: #f0f2f5;
  }
}
</style>